<template>
  <div class="main shadow-lg p-3 mb-5 bg-white rounded">
    <h1 class="text-center title">注册</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        label="用户名"
        prop="name"
      >
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item
        label="密码"
        prop="pass"
      >
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="确认密码"
        prop="checkPass"
      >
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          @click="submitForm('ruleForm')"
        >注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="goLogin('ruleForm')"
        >已有账号？去登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    var checkName = (rule, value, callback) => {
      var userName_reg = /^[a-zA-Z]{4,20}$/;
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      setTimeout(() => {
        if (!userName_reg.test(value)) {
          callback(new Error('请输入4-20位的字母'));
        } else {
          callback();
        }
      }, 500);
    };
    var validatePass = (rule, value, callback) => {
      var userpwd_reg = /^[a-zA-Z0-9]{6,12}$/;
      if (value === '') {
        return callback(new Error('请输入密码'));
      } else {
        setTimeout(() => {
          if (!userpwd_reg.test(value)) {
            callback(new Error('请输入6-12位的数组或字母'));
          } else {
            callback();
          }
        }, 500);
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        name: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        name: [
          { validator: checkName, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '恭喜你，注册成功',
            type: 'success'
          });
          this.$refs[formName].resetFields();
          this.$router.push('/login')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goLogin(formName) {
      this.$refs[formName].resetFields();
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.main {
  width: 500px;
  /* height: 300px; */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

.title {
  color: #409eff;
  margin-bottom: 30px;
}
</style>